﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JumpPads.FountainConnection;
using System.IO.Ports;
using System.Threading;
using System.IO;

namespace JumpPads
{
    class Program
    {
        static CPQClient _client = new CPQClient();
        static CommandPriority _priority = CommandPriority.kiosk;
        static String _code;

        static SerialPort _port;
        static string _lastData = "";
        static bool _stopped = false;
        static string state = "OFF";
        static int i = 0;


        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(@"C:\FountainData\kioskcode.txt");
            _code = sr.ReadLine();

            // Set up the serial port communication with the Arduino on COM3 at 9600 baud 
            _port = new SerialPort("COM5") { BaudRate = 9600, DataBits = 8, StopBits = StopBits.One, Parity = Parity.None};
            //  hook up the event for receiving the data 
            _port.DataReceived += new SerialDataReceivedEventHandler(_port_DataReceived);
            _port.Open();
            Console.Read();
            _port.Close();
        }

        static void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Byte data = (Byte)_port.ReadByte();
 
            //Console.WriteLine(data + ":" + data2);
            List<string> cmd = new List<string>();
            List<String> cmd_off = new List<string>();

            switch (data)
            {
                case 0x10:
                    cmd_off.Add("V2");
                    cmd_off.Add("V4");
                    cmd_off.Add("V6");
                    break;
                case 0x20:
                    cmd_off.Add("V7");
                    cmd_off.Add("V8");
                    cmd_off.Add("V9");
                    cmd_off.Add("V10");
                    break;
                case 0x30:
                    cmd_off.Add("VC");
                    cmd_off.Add("VR");
                    break;
                case 0x40:
                    cmd_off.Add("H2");
                    cmd_off.Add("H4");
                    cmd_off.Add("H6");
                    break;
                case 0x50:
                    cmd_off.Add("HC");
                    cmd_off.Add("HR");
                    break;
                case 0x60:
                    cmd_off.Add("H1");
                    cmd_off.Add("H3");
                    cmd_off.Add("H5");
                    break;
                case 0x70:
                    cmd_off.Add("V1");
                    cmd_off.Add("V3");
                    cmd_off.Add("V5");
                    break;
                case 0x80:
                    cmd_off.Add("V*");
                    cmd_off.Add("H*");
                    break;

                case 0x11:
                    cmd.Add("V2");
                    cmd.Add("V4");
                    cmd.Add("V6");
                    break;
                case 0x21:
                    cmd.Add("V7");
                    cmd.Add("V8");
                    cmd.Add("V9");
                    cmd.Add("V10");
                    break;
                case 0x31:
                    cmd.Add("VC");
                    cmd.Add("VR");
                    break;
                case 0x41:
                    cmd.Add("H2");
                    cmd.Add("H4");
                    cmd.Add("H6");
                    break;
                case 0x51:
                    cmd.Add("HC");
                    cmd.Add("HR");
                    break;
                case 0x61:
                    cmd.Add("H1");
                    cmd.Add("H3");
                    cmd.Add("H5");
                    break;
                case 0x71:
                    cmd.Add("V1");
                    cmd.Add("V3");
                    cmd.Add("V5");
                    break;
                case 0x81:
                    cmd.Add("V*");
                    cmd.Add("H*");
                    break;
            }


            Console.WriteLine("On States");
            foreach (String command in cmd)
            {
                Console.WriteLine(command);
            }

            Console.WriteLine("Off States");
            foreach (String command in cmd_off)
            {
                Console.WriteLine(command);
            }

            _client.SendCommand("on", cmd, _priority, _code);
            _client.SendCommand("off", cmd_off, _priority, _code);

            //Thread.Sleep(2000);
        }
    }
}
